﻿using UnityEngine;

namespace UFrame
{
    public class USpriteAnimation : MonoBehaviour
    {
        public SpriteRenderer mSR;
        public float mTimer;

        public Sprite[] mSpriteAry;
        private int mIndex = 0;
        private float mStarTimer;

        private bool mIsPlay = false;
        public bool mStartPlay = true;

        // Use this for initialization
        void Start()
        {
            if (mSR == null)
            {
                mSR = GetComponent<SpriteRenderer>();
            }

            if (mStartPlay)
            {
                Play();
            }
        }

        // Update is called once per frame
        void Update()
        {
            if (mIsPlay)
            {
                if (mSpriteAry.Length == 0)
                {
                    return;
                }

                if (mSR == null)
                {
                    return;
                }

                if (mStarTimer <= Time.time)
                {
                    SetTime();
                    mIndex++;
                    if (mIndex >= mSpriteAry.Length)
                    {
                        mIndex = 0;
                    }

                    mSR.sprite = mSpriteAry[mIndex];
                }
            }
        }

        private void SetTime()
        {
            mStarTimer = Time.time + mTimer;
        }

        public void Play()
        {
            mIsPlay = true;
            SetTime();
        }

        public void StopPlay()
        {
            mIsPlay = false;
        }
    }
}